package cn.learn.listener;

import cn.learn.utils.commons.MailUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import javax.mail.MessagingException;
import java.util.Map;

public class MqListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        //如果队列中监听到了数据，获取数据发邮件
        byte[] messageBody = message.getBody();
        // 将字节数据转成map
        Map<String,String> map = JSON.parseObject(messageBody, Map.class);
        // 获取队列中的消息
        String email = map.get("email");
        String emailMsg = map.get("emailMsg");
        //发邮件
        try {
            MailUtils.sendMail(email,emailMsg);
            System.out.println("邮件发送成功！！！");
        } catch (MessagingException e) {
            System.out.println("邮件发送失败！！！！");
        }
    }

}
